package com.zp.base.impl.examMaintenance;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.api.vo.ExamTypeVo;
import com.zp.base.api.vo.ExamVsClinicVo;
import com.zp.base.entity.examMaintenance.ExamType;
import com.zp.base.entity.examMaintenance.bo.ExamTypeBo;
import com.zp.base.entity.examMaintenance.bo.ExamVsClinicBo;
import com.zp.base.mapper.examMaintenance.ExamTypeMapper;
import com.zp.base.service.examMaintenance.IExamTypeService;
import com.zp.base.service.examMaintenance.IExamVsClinicService;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检查项目类型exam_typeService业务层处理
 *
 * @author zhang peng
 * @ date 2023-05-25
 */
@RequiredArgsConstructor
@Service
public class ExamTypeServiceImpl implements IExamTypeService {

    private final ExamTypeMapper baseMapper;

    private final IExamVsClinicService examVsClinicService;

    /**
     * 查询检查项目类型exam_type
     */
    @Override
    public ExamTypeVo queryById(String examTypeId) {
        return baseMapper.selectVoById(examTypeId);
    }

    /**
     * @param pageQuery
     * @return
     * @Author zhangpeng
     * @Description // 查询检查项目类型exam_type列表(分页)
     * @ date 9:32 2023/5/25
     * @Param bo
     **/
    @Override
    public TableDataInfo<ExamTypeVo> queryPageList(ExamTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamType> lqw = buildQueryWrapper(bo);
        Page<ExamTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        ExamVsClinicBo examVsClinicBo = new ExamVsClinicBo();
        examVsClinicBo.setOrgId(bo.getOrgId());
        examVsClinicBo.setDelFlag("0");
        List<ExamVsClinicVo> examVsClinicVos = examVsClinicService.queryList(examVsClinicBo);
        result.getRecords().forEach(examTypeVo -> {
            List<ExamVsClinicVo> collect = examVsClinicVos.stream().filter(x -> StringUtils.equals(x.getExamId(), examTypeVo.getExamTypeId())).collect(Collectors.toList());
            examTypeVo.setExamVsClinicVoList(collect);
        });
        return TableDataInfo.build(result);
    }


    /**
     * 查询检查项目类型exam_type列表(不分页)
     */
    @Override
    public List<ExamTypeVo> queryList(ExamTypeBo bo) {
        LambdaQueryWrapper<ExamType> lqw = buildQueryWrapper(bo);
        List<ExamTypeVo> examTypeVos = baseMapper.selectVoList(lqw);
        ExamVsClinicBo examVsClinicBo = new ExamVsClinicBo();
        examVsClinicBo.setOrgId(bo.getOrgId());
        examVsClinicBo.setDelFlag("0");
        List<ExamVsClinicVo> examVsClinicVos = examVsClinicService.queryList(examVsClinicBo);
        examTypeVos.forEach(examTypeVo -> {
            List<ExamVsClinicVo> collect = examVsClinicVos.stream().filter(x -> StringUtils.equals(x.getExamId(), examTypeVo.getExamTypeId())).collect(Collectors.toList());
            examTypeVo.setExamVsClinicVoList(collect);
        });
        return examTypeVos;
    }

    private LambdaQueryWrapper<ExamType> buildQueryWrapper(ExamTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ExamType::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), ExamType::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ExamType::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformedBy()), ExamType::getPerformedBy, bo.getPerformedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), ExamType::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getParentIds()), ExamType::getParentIds, bo.getParentIds());
        lqw.eq(bo.getCreateTime() != null, ExamType::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getUpdateTime() != null, ExamType::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ExamType::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ExamType::getDelFlag, bo.getDelFlag());
        lqw.orderByAsc(ExamType::getLevel, ExamType::getSort);
        return lqw;
    }

    /**
     * 新增检查项目类型exam_type
     */
    @Override
    public Boolean insertByBo(ExamTypeBo bo) {
        ExamType add = BeanUtil.toBean(bo, ExamType.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExamTypeId(add.getExamTypeId());
        }
        // 添加缓存
        CacheUtils.put(CacheNames.EXAM_TYPE, add.getExamTypeId(), add);
        return flag;
    }

    /**
     * 修改检查项目类型exam_type
     */
    @Override
    public Boolean updateByBo(ExamTypeBo bo) {
        ExamType update = BeanUtil.toBean(bo, ExamType.class);
        validEntityBeforeSave(update);
        boolean b = baseMapper.updateById(update) > 0;
        CacheUtils.evict(CacheNames.EXAM_TYPE, update.getExamTypeId());
        CacheUtils.put(CacheNames.EXAM_TYPE, update.getExamTypeId(), update);
        return b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检查项目类型exam_type
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        ids.forEach(id -> CacheUtils.evict(CacheNames.EXAM_TYPE, id));
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void refreshCache() {
        List<ExamType> examTypes = baseMapper.selectList();
        List<ExamTypeVo> examTypeVoList = BeanUtil.copyToList(examTypes,ExamTypeVo.class);
        examTypeVoList.forEach(examType -> CacheUtils.put(CacheNames.EXAM_TYPE, examType.getExamTypeId(), examType));
    }
}
